<?php

namespace app\teacher\model;

use think\Model;
use app\common\model\Teacher as TeacherModel;

class Teacher extends TeacherModel
{
    /**
     * 新增教师
     */
    public function add(array $data)
    {
        return $this->allowField(true)->save($data);
    }

    public function busy()
    {
        return $this->hasOne(TeacherBusy::class, 'teacher_id', 'teacher_id');
    }


    /**
     * 教师信息
     */
    public static function getTeacher($teacherId)
    {
        $teacher = self::with(['teacherCourse' => function ($query) {
            $query->with(['language', 'grade']);
        }, 'tag', 'busy', 'images', 'experience'])->find($teacherId);
        if (!$teacher) {
            static::errorMsg(20000);
        }
        //加入天数
        $teacher['day'] = ceil((time() - strtotime($teacher->createtime)) / 86400);
        $teacherCourseId = TeacherCourse::getColumnByTeacherId($teacherId, 'teacher_course_id');
        //累积课时
        $times = Course::getFinishTimes($teacherCourseId);
        $teacher['hour'] = Course::getHours($times);
        //累积课次
        $teacher['course_count'] = count($times);
        //累积订单
        $teacher['order_count'] = CourseOrder::getFinishCount($teacherCourseId);
        //未读消息数量
        $teacher['reading_count'] = RemindCourse::getReadingByTeacherId($teacherId) + RemindOrder::getReadingByTeacherId($teacherId);
        //课程评分、满意度
        $comment = CourseComment::score($teacherCourseId);
        $teacher['com_score'] = $comment['com_score'];
        $teacher['result_like'] = $comment['result_like'];
        $teacher['service_like'] = $comment['service_like'];
        return $teacher;
    }

    public static function getValueByTeacherId($teacherId, $value)
    {
        return self::where('teacher_id', $teacherId)->value($value);
    }


}
